<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="javatime" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<% String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE html>
<html>
<head>
    <base href=<%=basePath%>
    <%@include file="../common_css.jsp"%>
</head>
<body class="childrenBody">
<form class="layui-form" style="width:80%;">
    <input type="hidden" name="inputCusId" id="inputCusId" value="${cusDrive.cusId}">
    <input type="hidden" name="inputCarId" id="inputCarId" value="${cusDrive.carId}">
    <input type="hidden" name="inputDriveType" id="inputDriveType" value="${cusDrive.driveType}">
    <input type="hidden" name="inputUserId" id="inputUserId" value="${cusDrive.userId}">
    <div class="layui-form-item layui-row">
        <div class="layui-col-xs6">
            <label class="layui-form-label">客户姓名</label>
            <div class="layui-input-block">
                <select name="cusId"  id="cusId" lay-filter="cusId"
                        <c:if test="${cusDrive.cusId != null}">disabled</c:if>
                >
                    <option value="">请选择</option>
                </select>
            </div>
        </div>
        <div class="layui-col-xs6">
            <label class="layui-form-label">体验人姓名</label>
            <div class="layui-input-block">
                <input type="text" class="layui-input"
                       lay-verify="required" name="driveName" id="driveName"  value="${cusDrive.driveName}"
                       <c:if test="${cusDrive.cusId != null}">readonly</c:if>
                >
            </div>
        </div>
    </div>


    <div class="layui-form-item layui-row">
        <div class="layui-col-xs6">
            <label class="layui-form-label">手机号</label>
            <div class="layui-input-block">
                <input type="text" class="layui-input"
                       lay-verify="phone" name="phone" id="phone"  value="${cusDrive.phone}"
                       <c:if test="${cusDrive.cusId != null}">readonly</c:if>
                >
            </div>
        </div>
        <div class="layui-col-xs6">
            <label class="layui-form-label">试驾车辆</label>
            <div class="layui-input-block">
                <select name="carId"  id="carId"
                        <c:if test="${cusDrive.cusId != null}">disabled</c:if>
                >
                </select>
            </div>
        </div>
    </div>
    <div class="layui-form-item layui-row">
        <div class="layui-col-xs6">
            <label class="layui-form-label">试驾类型</label>
            <div class="layui-input-block">
                <select name="driveType"  id="driveType"
                        <c:if test="${cusDrive.cusId != null}">disabled</c:if>
                >
                </select>
            </div>
        </div>
        <div class="layui-col-xs6">
            <label class="layui-form-label">试驾时间</label>
            <div class="layui-input-block">
                <input type="text" class="layui-input" id="driveTime" name="driveTime"
                       value="${cusDrive.formattedDate}"
                       <c:if test="${cusDrive.cusId != null}">disabled</c:if>
                >
            </div>
        </div>
    </div>
    <div class="layui-form-item layui-row">
        <div class="layui-col-xs6">
            <label class="layui-form-label">主体验人</label>
            <input type="checkbox" lay-filter="primary" name="primary" title="主" <c:if test="${cusDrive.primary==true}">checked</c:if>
                   <c:if test="${cusDrive.cusId != null}">disabled</c:if>
            >
        </div>
        <div class="layui-col-xs6">
            <label class="layui-form-label">分配员工</label>
            <div class="layui-input-block">
<%--                <input name="userId" class="layui-input" value="${cusDrive.userId}" id="userId" readonly>--%>
                <select name="userId"  id="userId" disabled>
                </select>
            </div>
        </div>
    </div>
    <br/>
    <c:if test="${cusDrive.cusId == null}">
        <div class="layui-form-item layui-row layui-col-xs12">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-lg" lay-submit=""
                        lay-filter="addOrUpdateCustomerServe">确认
                </button>
                <button class="layui-btn layui-btn-lg layui-btn-normal">取消</button>
            </div>
        </div>
    </c:if>

</form>
<script type="text/javascript">
    layui.use(['form', 'layer','laydate'], function () {
        var form = layui.form,
            layer = parent.layer === undefined ? layui.layer : top.layer,
            $ = layui.jquery;
        var laydate = layui.laydate;
        laydate.render({
            elem: '#driveTime',
            type: 'datetime',
            min:0,
            max:7,
        });

        $.post("customer/findCustomerByState",{state:2},function (res) {
            for(var i=0;i<res.length;i++){
                if($("input[name='inputCusId']").val() == res[i].id){
                    $("#cusId").append("<option value=\""+res[i].id+"\"  selected >"+res[i].name+"</option>")
                }else{
                    $("#cusId").append("<option value=\""+res[i].id+"\"   >"+res[i].name+"</option>")
                }
            }
            layui.form.render("select");
        });
        $.post("car/findCarAll",function (res) {
            for(var i=0;i<res.length;i++){
                if($("input[name='inputCarId']").val() == res[i].id){
                    $("#carId").append("<option value=\""+res[i].id+"\" selected  >"+res[i].series+" "+res[i].model+"</option>");
                }else{
                    $("#carId").append("<option value=\""+res[i].id+"\"   >"+res[i].series+" "+res[i].model+"</option>");
                }
            }
            layui.form.render("select");
        });
        $.post("dictionaries/findDriveType",function (res) {
            for(var i=0;i<res.length;i++){
                if($("input[name='inputDriveType']").val() == res[i].valueId){
                    $("#driveType").append("<option value=\""+res[i].valueId+"\" selected  >"+res[i].valueName+"</option>");
                }else{
                    $("#driveType").append("<option value=\""+res[i].valueId+"\"   >"+res[i].valueName+"</option>");
                }

            }
            layui.form.render("select");
        });


        if($("input[name='inputUserId']").val()!=''){
            $.post("user/findUser",function (res) {
                for(var i=0;i<res.length;i++) {
                    if ($("input[name='inputUserId']").val() == res[i].id) {
                        $("#userId").append("<option value=\"" + res[i].id + "\" selected  >" + res[i].name + "</option>");
                    } else {
                        $("#userId").append("<option value=\"" + res[i].id + "\"   >" + res[i].name + "</option>");
                    }
                }
                layui.form.render("select");
            })
        }

        form.on('submit(addOrUpdateCustomerServe)',function (data) {
            var index= top.layer.msg("数据提交中,请稍后...",{icon:16,time:false,shade:0.8});
            var url = "cusDrive/saveCusDrive";
            $.post(url,data.field,function (res) {
                if(res=='success'){
                    top.layer.msg("操作成功");
                    top.layer.close(index);
                    layer.closeAll("iframe");
                    // 刷新父页面
                    parent.location.reload();
                }
            });
            return false;
        });
        form.on('select(cusId)', function(data){
            $.post("user/findUserByCustomerId",{cusId:data.value},function (data) {
                $("#userId").append("<option value=\""+data.id+"\" selected  >"+data.name+"</option>");
                layui.form.render("select");
            })
        });

        form.on('checkbox(primary)', function(data){
            if (data.elem.checked==true){
                $("#driveName").attr("disabled","disabled");
                $("#driveName").val($("#cusId option:selected").text())
            }else {
                $("#driveName").removeAttr("disabled");
                $("#driveName").val("");
            }
        });
    });
</script>
</body>
</html>